// lib/models/user.dart
import 'package:json_annotation/json_annotation.dart';

import '../utils/json_converters.dart';

part 'user.g.dart';
//  flutter pub run build_runner build
@JsonSerializable(
  explicitToJson: true,
  includeIfNull: false,
  checked: true,
)

class User {
  @IntToStringConverter() // <--- 在这里应用转换器
  @JsonKey(name: 'UserID', defaultValue: '0') // **修改：name 改为 'UserID'**
  final String userId;

  @JsonKey(name: 'Username', defaultValue: '') // **修改：name 改为 'Username'**
  final String username;

  @JsonKey(name: 'Avatar', defaultValue: '') // **修改：name 改为 'Avatar'**
  final String avatar;

  @JsonKey(name: 'Email', defaultValue: '') // **修改：name 改为 'Email'**
  final String email;

  @JsonKey(name: 'Phone', defaultValue: '') // **修改：name 改为 'Phone'**
  final String phone;

  @JsonKey(name: 'Token', defaultValue: '') // **修改：name 改为 'Token'**
  final String token;

  User({
    required this.userId,
    required this.username,
    this.avatar = '',
    this.email = '',
    this.phone = '',
    this.token = '',
  });

  factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
  Map<String, dynamic> toJson() => _$UserToJson(this);

  String get userIdValue => userId;

  static User empty() => User(
    userId: '0',
    username: '',
  );

  bool get isValid => userId != '0' && username.isNotEmpty;

  @override
  String toString() => 'User(userId:$userId, username:$username, avatar:$avatar)'; // 增加 avatar 便于调试

  // 用于深拷贝
  User copyWith({
    String? userId,
    String? username,
    String? avatar,
    String? email,
    String? phone,
    String? token,
  }) {
    return User(
      userId: userId ?? this.userId,
      username: username ?? this.username,
      avatar: avatar ?? this.avatar,
      email: email ?? this.email,
      phone: phone ?? this.phone,
      token: token ?? this.token,
    );
  }
}